package com.lune.job.controller;


import com.lune.job.pojo.Profession;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IProfessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/profession")
@Api(tags = "行业管理")
public class ProfessionController {

    @Autowired
    private IProfessionService professionService;

    @ApiOperation(value = "添加行业")
    @PostMapping("/")
    public RespBean create(@RequestBody Profession profession) {
        if (profession.getParent_id() == null) {
            profession.setParent_id(0);
        }
        if (professionService.save(profession)) {
            return RespBean.success("添加行业成功");
        } else {
            return RespBean.error("添加行业失败");
        }
    }

    @ApiOperation(value = "删除行业")
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable Integer id) {
        if (professionService.removeById(id)) {
            return RespBean.success("删除行业成功");
        } else {
            return RespBean.error("删除行业失败");
        }
    }

    @ApiOperation(value = "批量删除行业")
    @DeleteMapping("/")
    public RespBean deleteByIds(Integer[] ids) {
        if (professionService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除行业成功");
        } else {
            return RespBean.error("批量删除行业失败");
        }
    }

    @ApiOperation(value = "修改行业")
    @PutMapping("/")
    public RespBean update(@RequestBody Profession profession) {
        if (professionService.updateById(profession)) {
            return RespBean.success("修改行业成功");
        } else {
            return RespBean.error("修改行业失败");
        }
    }

    @ApiOperation(value = "根据id查询行业")
    @GetMapping("/detail")
    public RespBean detail(Integer id) {
        Profession professions = professionService.detailById(id);
        return RespBean.success("查询行业成功", professions);
    }

    @ApiOperation(value = "查询所有行业")
    @GetMapping("/query")
    public List<Profession> queryPage(Profession profession) {
        return professionService.queryAll();

    }

    @ApiOperation(value = "树形结构查询行业")
    @GetMapping("/tree")
    public RespBean tree() {
        List<Profession> professionList = professionService.tree();
        List<Map<String, Object>> mapList = new ArrayList<>();
        professionList.forEach(profession -> {

            if (profession.getParent_id() == 0) {
                //一级菜单 查找子菜单
                Map<String, Object> map = new HashMap<>();
                map.put("id", profession.getId());
                map.put("label", profession.getName());
                map.put("parent_id", profession.getParent_id());
                //构建children
                map.put("children", children(profession.getId(), professionList));
                mapList.add(map);
            }

        });
        return RespBean.success("树形结构查询成功 ", mapList);


    }

    //构建children
    public List<Map<String, Object>> children(Integer id, List<Profession> list) {
        List<Map<String, Object>> children = new ArrayList<>();
        for (Profession profession : list) {
            if (profession.getParent_id() == id) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", profession.getId());
                map.put("label", profession.getName());
                map.put("parent_id", profession.getParent_id());
                children.add(map);  //子菜单中添加数据

            }
        }
        return children;
    }

}
